package com.javatian.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javatian.common.ResultCode;
import com.javatian.dao.WorkerInfoDao;
import com.javatian.entity.WorkerInfo;
import com.javatian.service.WorkerInfoService;
import exception.CustomException;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.util.StringUtil;

import javax.annotation.Resource;
import java.util.List;

/**
 * 工作人员对外提供的服务的实现类
 */
@Service("workerInfoService")
public class WorkerInfoServiceImpl implements WorkerInfoService {

    @Resource
    private WorkerInfoDao workerInfoDao;
    /**
     * 根据姓名精确查询一条数据
     */
    @Override
    public WorkerInfo findByUserName(String username) {

        return workerInfoDao.findByUserName(username);
    }

    /**
     * 根据用户名和密码判断用户是否存在，如果存在，返回这个用户；否者返回错误信息
     */
    @Override
    public WorkerInfo login(String name, String password) {
        WorkerInfo workerInfo = workerInfoDao.findByUserName(name);
        if (workerInfo==null){
            throw new CustomException(ResultCode.USER_ACCOUNT_ERROR);
        }
        if (!SecureUtil.md5(password).equalsIgnoreCase((workerInfo.getPassword()))){
            throw new CustomException(ResultCode.USER_ACCOUNT_ERROR);
        }
        return workerInfo;
    }

    /**
     * 增加一条记录
     *
     * @param workerInfo
     */
    @Override
    public WorkerInfo add(WorkerInfo workerInfo) {
        //唯一校验
        int count = workerInfoDao.checkRepeat("name", workerInfo.getName(), null);
        if (count > 0){
            throw new CustomException("2001","用户名\""+workerInfo.getName()+"\"已存在");
        }
        if (StringUtil.isEmpty(workerInfo.getPassword())){
            //默认密码
            workerInfo.setPassword(SecureUtil.md5("123456"));
        }else {
            workerInfo.setPassword(SecureUtil.md5(workerInfo.getPassword()));
        }
        workerInfoDao.insertSelective(workerInfo);
        return workerInfo;
    }

    /**
     * 删除一条记录
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        workerInfoDao.deleteByPrimaryKey(id);
    }

    /**
     * 修改一条记录
     *
     * @param workerInfo
     */
    @Override
    public void update(WorkerInfo workerInfo) {
        workerInfoDao.updateByPrimaryKeySelective(workerInfo);
    }

    /**
     * 根据主键查询一条记录
     *
     * @param id
     */
    @Override
    public WorkerInfo findById(Long id) {
        return workerInfoDao.selectByPrimaryKey(id);
    }

    /**
     * 查询所有工作人员记录
     */
    @Override
    public List<WorkerInfo> findAll() {
        return workerInfoDao.findByName("all");
    }

    /**
     * 根据条件分页查询
     *
     * @param name     用户名
     * @param pageNum  第几页
     * @param pageSize 每页数量
     * @return 当前页的List列表
     */
    @Override
    public PageInfo<WorkerInfo> findPage(String name, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<WorkerInfo> all = workerInfoDao.findByName(name);
        return PageInfo.of(all);
    }

    /**
     * 工作人员总数
     */
    @Override
    public Integer count() {
        return workerInfoDao.count();
    }
}
